<? /*
**************************************************
*	Crisses Mailinglist	       					 *
**************************************************
|                                                |
|   See install.txt for copyright & license info.|
-------------------------------------------------
*/ ?><?
function subscribe($email){	
	global $conn, $db, $prefix, $mail, $l_notvalid, $l_added, $l_alreadysubscribed, $l_notactivated, $yourname, $youremail, $msg_activate2, $msg_activate1, $l_registration, $l_activate;
	
	$key = randomkeys(32);
	
	if(!ereg("^.+@.+\\..+$", $email)){
         $display_block =  $l_notvalid;
	}
	
	$sql = "SELECT * 
			FROM ".$prefix."subscribers
			WHERE email = '".$email."'";
	$result = mysql_query($sql, $conn)or die(mysql_error());
	$num = mysql_num_rows($result);
	
	$sql = "SELECT * 
			FROM ".$prefix."activation
			WHERE email = '".$email."'";
	$result = mysql_query($sql, $conn)or die(mysql_error());
	$num2 = mysql_num_rows($result);
	
	if($num == 1){
		$display_block = $l_alreadysubscribed.".";			
		
	}elseif($num2 == 1){
		$display_block = $l_notactivated;
		
	}else{
		$sql = "INSERT INTO `".$prefix."activation`
					values('', '".$email."', '".time()."', '".$key."')";
		
		$msg_activate = $msg_activate1."<a href=\"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?k=".$key."\"><b>".$l_activate."</b></a><br/><br/>".$msg_activate2;
	
	
		$mail->addPart($msg_activate, 'text/html');
		$mail->send($email, '"'.$yourname.'" <'.$youremail.'>', $l_registration);
		$mail->close();
		
		$result = mysql_query($sql, $conn)or die(mysql_error());
		
		$display_block = $l_added;
	
	}
	return $display_block;
}

function activate($key){
	global $conn, $db, $prefix, $mail, $l_confirmed, $l_falseconfirmation;
	
	$key;
	
	$sql = "SELECT *
			FROM `".$prefix."activation`
			WHERE `key` = '".$key."'";
	
	echo "<br/>";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	
	$email = mysql_fetch_row($result);
	$email = $email[1];
	
	echo $result[email];
	
	$num = mysql_num_rows($result);

	if($num == "1"){	
		$sql = "INSERT INTO ".$prefix."subscribers
				VALUES('', '".$email."', '".time()."')";			
		$result = mysql_query($sql, $conn)or die(mysql_error());
		
		$sql = "DELETE 
				FROM ".$prefix."activation
				WHERE `key` = '".$key."'";
				
		$result = mysql_query($sql,$conn)or die(mysql_error());		
		
		$display_block .= $l_confirmed;
		
	}elseif($num == "0"){
		
		$display_block .= $l_falseconfirmation;
		
	}

	return $display_block;
}

function unsubscribe($email){
	global $conn, $prefix, $l_notsubscribed,$l_removed, $l_deleted;

	$sql ="SELECT *
			FROM `".$prefix."subscribers`
			WHERE `email` = '".$email."'"; 
	$result = mysql_query($sql);	
	$num = mysql_num_rows($result);
	
	$sql = "DELETE 
			FROM `".$prefix."subscribers`
			WHERE `email` = '".$email."'"; 
	
	$result = mysql_query($sql, $conn)or die(mysql_error());
	
	if($num == 0){
		
		echo $l_notsubscribed;
	
	}else{	
	
		echo $l_deleted.".";
	}
	
	return $display_block;
}

function randomkeys($length){

	$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
	
	$key = "";
	
	for($i=0;$i<$length;$i++){
	
		 $key .= $pattern{rand(0,35)};
	}
			
	return $key;
}

function listsubscribers(){
	global $conn, $db, $language, $prefix, $l_id, $l_email, $l_date, $l_delete, $l_page;
	include($language);
	
	mysql_select_db($db);
	$page = explode("&",$_SERVER['QUERY_STRING']);
	$page = explode("=", $page[1]);
	$page = $page[1];
	
	$start = $page * 20 - 20;
	
	if(!isset($page)){
		$start = 0;
		$page = 1;
		
	}	
	
	$sql = "SELECT `id`, `email`, `date`
			FROM ".$prefix."subscribers
			ORDER BY `id` ASC
			LIMIT ".$start.",20";
			
	$result = mysql_query($sql, $conn)or die(mysql_error());	
	
	$num = mysql_num_rows($result);	
	
	$sql2 ="SELECT `id`, `email`, `date`
			FROM ".$prefix."subscribers";
	$result2 = mysql_query($sql2, $conn)or die(mysql_error());	 
	$num2 = mysql_num_rows($result2);	
	
	//calculate amount of pages, always round up
	$pages = ceil($num2 / 20);
	
	$display_block = "<table>
					  	<tr>
							<td width=\"25\" bgcolor=\"#BCBCBA\"><b>".$l_id."</b></td>
							<td width=\"200\" bgcolor=\"#BCBCBA\"><b>".$l_email."</b></td>
							<td width=\"100\" bgcolor=\"#BCBCBA\"><b>".$l_date."</b></td>
							<td bgcolor=\"#BCBCBA\"><b>".$l_delete."</b></td>
						</tr>";	
						
	$i =0;
	
	while($i < $num){
		
		$row = mysql_fetch_array($result);
		
		$display_block .= "<tr>
							<td>".$row['id']."</td>
							<td>".$row['email']."</td>
							<td>".date("d.m.Y", $row['date'])."</td>
							<td align=\"center\"><a href=\"admin.php?p=6&id=".$row['id']."\"><img src=\"images/delete.png\" alt=\"delete\" border=\"0\"></a></td>
						</tr>";
		$i++;
							
	}
	
	$display_block .= "</table><br/>"; 
	
	$p=1;

	$display_block .= $l_page.":";
	$pages;
	do{	
		
		if($page == $p){
			$display_block .= $p;
			$p++;
			$pages;
			
			}if(($p > $pages)||($pages == 1)){
				break;
			
			
		}
	
		$display_block .= " <a href=\"admin.php?p=3&l=".$p."\">".$p."</a> ";
		$p++;
		
	}while($pages >= $p);
	
	return $display_block;
}

function removebulk(){
	global $conn, $prefix;
	
	$timestamp = time() - 172800;
	
	$sql = "DELETE FROM ".$prefix."activation
			WHERE `timestamp` < ".$timestamp;
			
	$result = mysql_query($sql, $conn)or die(mysql_error());

}

function remove(){
	global $conn, $prefix, $l_verwijderd;
	#echo $_SERVER['QUERY_STRING'];
	$id = explode('&',$_SERVER['QUERY_STRING']);
	$id = explode('=', $id[1]);

	
	$sql = "DELETE FROM ".$prefix."subscribers
			WHERE id = ".$id[1];
			
	$result = mysql_query($sql,$conn)or die(mysql_error());		
	
	$display_block = $l_verwijderd;
	
	return $display_block;


}
?>